package com.platform.util;

import com.platform.constant.ApiConstant;
import com.platform.entity.GoodsVo;
import com.platform.entity.UserVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.List;


/**
 * 作者: @author Harmon <br>
 * 时间: 2017-08-11 08:58<br>
 * 描述: ApiUserUtils <br>
 */
@Component
public class ApiUserUtils {

    private static String appId;

    private static String secret;

    private static String mchId;

    private static String tradeType;

    private static String notifyUrl;

    private static String paySignKey;

    private static String uniformorder;

    private static String code;

    private static String refundUrl;

    private static String webAccessTokenhttps;

    private static String userMessage;

    private static String accessToken;

    private static String wXACodeUnlimit;

    private static String orgPay;

    // --------------------------mp-----------------------------
    private static String createMenuUrl;

    private static String ticketUrl;

    private static String certName;

    private static String templateMsgUrl;

    //替换字符串
    public static String getCode(String REDIRECT_URI, String SCOPE, Integer STATE) {
        return String.format(code, appId, REDIRECT_URI, SCOPE, STATE);
    }

    //替换字符串
    public static String getWebAccess(String CODE) {
        return String.format(webAccessTokenhttps,appId,secret,CODE);
    }

    //替换字符串
    public static String getUserMessage(String access_token, String openid) {
        return String.format(userMessage, access_token, openid);
    }


    //替换字符串
    public static String getAccessToken() {
        return String.format(accessToken,appId,secret);
    }

    public static String getWXACodeUnlimit(String accessToken, String scene) {
        return String.format(wXACodeUnlimit, accessToken, scene);
    }

    @Value("${wx.refundUrl}")
    public  void setRefundUrl(String refundUrl) {
        ApiUserUtils.refundUrl = refundUrl;
    }

    public static String getRefundUrl() {
        return refundUrl;
    }

    @Value("${wx.code}")
    public  void setCode(String code) {
        ApiUserUtils.code = code;
    }

    @Value("${wx.webAccessTokenhttps}")
    public  void setWebAccessTokenhttps(String webAccessTokenhttps) {
        ApiUserUtils.webAccessTokenhttps = webAccessTokenhttps;
    }

    @Value("${wx.appId}")
    public  void setAppId(String appId) {
        ApiUserUtils.appId = appId;
    }

    @Value("${wx.secret}")
    public  void setSecret(String secret) {
        ApiUserUtils.secret = secret;
    }

    @Value("${wx.userMessage}")
    public  void setUserMessage(String userMessage) {
        ApiUserUtils.userMessage = userMessage;
    }

    @Value("${wx.accessToken}")
    public  void setAccessToken(String accessToken) {
        ApiUserUtils.accessToken = accessToken;
    }

    @Value("${wx.wXACodeUnlimit}")
    public  void setwXACodeUnlimit(String wXACodeUnlimit) {
        ApiUserUtils.wXACodeUnlimit = wXACodeUnlimit;
    }

    public static String getCreateMenuUrl(String accessToken) {
        return String.format(createMenuUrl, accessToken);
    }

    @Value("${weixin.mp.createMenuUrl}")
    public void setCreateMenuUrl(String createMenuUrl) {
        ApiUserUtils.createMenuUrl = createMenuUrl;
    }

    public static String getAppId() {
        return appId;
    }

    public static String getSecret() {
        return secret;
    }

    public static String getMchId() {
        return mchId;
    }
    @Value("${wx.mchId}")
    public void setMchId(String mchId) {
        ApiUserUtils.mchId = mchId;
    }

    public static String getTradeType() {
        return tradeType;
    }

    @Value("${wx.tradeType}")
    public void setTradeType(String tradeType) {
        ApiUserUtils.tradeType = tradeType;
    }

    public static String getNotifyUrl() {
        return notifyUrl;
    }

    @Value("${wx.notifyUrl}")
    public void setNotifyUrl(String notifyUrl) {
        ApiUserUtils.notifyUrl = notifyUrl;
    }

    public static String getPaySignKey() {
        return paySignKey;
    }

    @Value("${wx.paySignKey}")
    public void setPaySignKey(String paySignKey) {
        ApiUserUtils.paySignKey = paySignKey;
    }

    public static String getUniformorder() {
        return uniformorder;
    }

    @Value("${wx.uniformorder}")
    public void setUniformorder(String uniformorder) {
        ApiUserUtils.uniformorder = uniformorder;
    }

    public static String getTicketUrl(String accessToken) {
        return String.format(ticketUrl, accessToken);
    }

    @Value("${weixin.mp.ticketUrl}")
    public void setTicketUrl(String ticketUrl) {
        ApiUserUtils.ticketUrl = ticketUrl;
    }

    public static String getCertName() {
        return certName;
    }

    @Value("${wx.certName}")
    public void setCertName(String certName) {
        ApiUserUtils.certName = certName;
    }

    public static String getTemplateMsgUrl(String accessToken) {
        return String.format(templateMsgUrl, accessToken);
    }

    @Value("${weixin.mp.templateMsgUrl}")
    public void setTemplateMsgUrl(String templateMsgUrl) {
        ApiUserUtils.templateMsgUrl = templateMsgUrl;
    }

    public static String getOrgPay() {
        return orgPay;
    }

    @Value("${wx.orgPay}")
    public void setOrgPay(String orgPay) {
        ApiUserUtils.orgPay = orgPay;
    }

    public static void showPrice(UserVo loginUser, List<GoodsVo> goodsList) {
        for(GoodsVo vo : goodsList){
            vo.setRetail_price(getActualPrice(loginUser, vo));
        }
    }

    public static BigDecimal getActualPrice(UserVo loginUser, GoodsVo vo) {
        if(ApiConstant.VIP_USER == loginUser.getUser_level_id()){
            return vo.getVip_price();
        }else if(ApiConstant.SHOP_USER == loginUser.getUser_level_id()){
            return vo.getShop_price();
        }else if(ApiConstant.AGENT_USER == loginUser.getUser_level_id()){
            return vo.getAgent_price();
        }else if(ApiConstant.REGION_USER == loginUser.getUser_level_id()){
            return vo.getRegion_price();
        }else if(ApiConstant.COOP_USER == loginUser.getUser_level_id()){
            return vo.getCoop_price();
        }
        return vo.getRetail_price();
    }

    public static String getLevelName(Integer user_level_id) {
        if(ApiConstant.RETAIL_USER == user_level_id){
            return "游客";
        }else if(ApiConstant.VIP_USER == user_level_id){
            return "VIP";
        }else if(ApiConstant.SHOP_USER == user_level_id){
            return "合伙人";
        }else if(ApiConstant.AGENT_USER == user_level_id){
            return "大区合伙人";
        }else if(ApiConstant.REGION_USER == user_level_id){
            return "联合创始人";
        }else if(ApiConstant.COOP_USER == user_level_id){
            return "战略合作商";
        }else if(ApiConstant.COMPANY_USER == user_level_id){
            return "总公司";
        }
        return "未知";
    }

    //返回微信服务
    public static String setXml(String return_code, String return_msg) {
        return "<xml><return_code><![CDATA[" + return_code + "]]></return_code><return_msg><![CDATA[" + return_msg + "]]></return_msg></xml>";
    }

    //模拟微信回调接口
    public static String callbakcXml(String orderNum) {
        return "<xml><appid><![CDATA[wx2421b1c4370ec43b]]></appid><attach><![CDATA[支付测试]]></attach><bank_type><![CDATA[CFT]]></bank_type><fee_type><![CDATA[CNY]]></fee_type> <is_subscribe><![CDATA[Y]]></is_subscribe><mch_id><![CDATA[10000100]]></mch_id><nonce_str><![CDATA[5d2b6c2a8db53831f7eda20af46e531c]]></nonce_str><openid><![CDATA[oUpF8uMEb4qRXf22hE3X68TekukE]]></openid> <out_trade_no><![CDATA[" + orderNum + "]]></out_trade_no>  <result_code><![CDATA[SUCCESS]]></result_code> <return_code><![CDATA[SUCCESS]]></return_code><sign><![CDATA[B552ED6B279343CB493C5DD0D78AB241]]></sign><sub_mch_id><![CDATA[10000100]]></sub_mch_id> <time_end><![CDATA[20140903131540]]></time_end><total_fee>1</total_fee><trade_type><![CDATA[JSAPI]]></trade_type><transaction_id><![CDATA[1004400740201409030005092168]]></transaction_id></xml>";
    }

    public static String getREDIRECT_URI(HttpServletRequest request, String uri){
        String scheme = request.getScheme();
        String serverName = request.getServerName();
        String path = request.getContextPath();
        String REDIRECT_URI = scheme + "://" + serverName + path +uri;
        return REDIRECT_URI;
    }


}
